import { Expose, Transform, Type } from "class-transformer";
import { IsNotEmpty, MaxLength, IsBoolean, IsOptional, ValidateIf, IsString, IsEnum } from "class-validator";
import { ApiProperty } from '@nestjs/swagger';
import { LOGIN_TYPE } from "../auth.constant";

// TODO message 修复信息不显示
export class AuthLoginDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString({ message: () => '必须为 string 类型'})
  @MaxLength(50)
  @ValidateIf(e => e.type === LOGIN_TYPE.NAME)
  readonly name: string;

  
  @ApiProperty()
  @IsNotEmpty()
  @Type(() => String)
  @IsString()
  @ValidateIf(e => e.type === LOGIN_TYPE.NAME)
  readonly password: string;

  @ApiProperty()
  @IsOptional()
  @IsEnum(LOGIN_TYPE, { message: '登录类型可选择：name|phone 两种类型', each: true })
  @Expose() // 导出后，可在controller 内使用
  @Transform(({ value }) => value ? value : 'name', { toClassOnly: true })
  readonly type: LOGIN_TYPE; // phone 或者 name。默认name

  @ApiProperty()
  @IsOptional()
  @IsBoolean()
  @ValidateIf(e => e.rememberMe !== "")
  readonly rememberMe?: boolean;

  @ApiProperty()
  @IsNotEmpty({ message: () => 'phone 不能为空'})
  @IsString()
  @MaxLength(11)
  @ValidateIf(e => e.type === LOGIN_TYPE.PHONE)
  readonly phone: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  @MaxLength(11)
  readonly captchaCode: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  @MaxLength(20)
  @ValidateIf(e => e.type === LOGIN_TYPE.NAME)
  readonly captchaId: string;
}
